var api = require('../../utils/api.js');
var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    zs: 1,
    tijiao:1,
    goods: [],
    goodstitle: '',
    addorder:'',
    show:0,
    date:'',
    multiArray: [[], []],
  },

  getallgoods(){
    wx.navigateTo({
      url: '/pages/my/allgoods',
    })
  },

  bindDateChange(ret){
    console.log(ret);

    this.setData({
        date:ret.detail.value
    })

  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  bindPickerChanged(ret){
      console.log(ret)
      var index=ret.detail.value;

      var that=this;
      var downs=this.data.downs;

      this.setData({
          down:downs[index]
      })


  },

  onLoad(options) {
    this.setData({
      id: options.id
    })

    var that=this;

    api.get({
        url: 'wxapp/index/getcodes',
        data: {},
        success: data => {
          console.log(data);
          var info = data.data;
          that.setData({
            downs: info.downs
          })
        }
      });
    

  },

  getmultiArray(){
    var arr = [];

    var list=this.data.goods;
    console.log(list);

	for(var i=0; i<list.length; i++){
		arr.push(list[i].name)
	}

    this.setData({
        multiArray:[arr]
    })
       
    this.getmultiArray1()
  },

  
  getmultiArray1(){
    var arr = [];

    var list=this.data.goods;
   
    var lists=list[0].down;

    console.log(lists);
	for(var i=0; i<lists.length; i++){
		arr.push(lists[i])
    }
    var multiArray=this.data.multiArray;

    console.log(arr);
    multiArray.push(arr)

    this.setData({
        multiArray:multiArray
    })
       

  },


  formSubmit(e) {
    console.log(e.detail.value)

    var that = this;
    var id = this.data.id;
    var data = e.detail.value;

    this.setData({
        tijiao:0
    })



    console.log(data)
    
    data.goodname =that.data.goodstitle ;
    data.dates=that.data.date;
    data.downs=this.data.down;
    

    api.post({
      url: 'wxapp/user/addtameorder',
      data: data,
      success: data => {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })

        if (data.code == 1) {
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/my/tameorder',
            })
          }, 1500)
        }else{

            that.setData({
                tijiao:1
            })
        
        }
      }
    });

  },

  onShow() {

    var that = this;

    let pages = getCurrentPages();
    let currPages = pages[pages.length-1]
    console.log('接收商品信息',currPages.data.goodstitle)
    this.setData({
        goodstitle:currPages.data.goodstitle,
        number:currPages.data.number,
    })
        



    var tamecodes=wx.getStorageSync('tamecodes');

    if(!tamecodes){

        wx.switchTab({
          url: '/pages/index/index',
        })
    }

    api.get({
      url: 'wxapp/index/test',
      data: {},
      success: data => {
          console.log(data);

          that.setData({
            show: data.data.show
          })
      }
    })

  },

  majorCLassesColumnChange(ret){
    console.log(ret);
    var index = ret.detail.column;
    var value = ret.detail.value;
    var that=this;

    if(index==0){
        var arr = [];
        var list=this.data.goods;
        var lists=list[value].down;
    
   
        for(var i=0; i<lists.length; i++){
            arr.push(lists[i])
        }
        var multiArray=that.data.multiArray;
    
        console.log(arr);
        multiArray[1]=arr;
    
        that.setData({
            multiArray:multiArray
        })
    }

       


  },

  bindPickerChange(ret) {
    console.log(ret);
    var index = ret.detail.value;
    var goods = this.data.multiArray;

    var a=index[0];
    var b=index[1];

        console.log(goods[0][a]);
        console.log(goods[1][b]);

    var good=goods[0][a]+' '+goods[1][b];

        this.setData({
            good:good
        })

  },



  radioChange(ret) {
    console.log(ret);
    this.setData({
        zs: ret.detail.value
    })
  }

})